#include"abstractlistmodel.h"
#include<QDebug>

AbstractListModel::AbstractListModel(QObject *parent)
    :QAbstractListModel(parent)
{

}

void AbstractListModel::addList(const AbstractList &list)
{
    beginInsertRows(QModelIndex(),rowCount(),rowCount());
    m_abstractList.append(list);
    endInsertRows();
}

void AbstractListModel::clearList()
{

    m_abstractList.clear();

}

void AbstractListModel::show()
{
    for(int i=0; i < m_abstractList.size(); i++)
        qDebug()<<m_abstractList[i].section_;
    qDebug()<<"new";
}

void AbstractListModel::remove()
{
    int row = 0;
    int count = m_abstractList.size();
    beginRemoveRows(QModelIndex(), row, row + count - 1);
    for(int i=row+count-1; i>row-1; --i){
        m_abstractList.removeAt(i);

    }
    endRemoveRows();
}

int AbstractListModel::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    return m_abstractList.count();
}

QVariant AbstractListModel::data(const QModelIndex &index, int role) const
{
  if(index.row()<0||index.row()>=m_abstractList.count())
      return QVariant();
  const AbstractList &abstractList=m_abstractList[index.row()];
  if(role==AbstractListRoles::SectionRole)
      return abstractList.getSection();
  return QVariant();
}

QHash<int, QByteArray> AbstractListModel::roleNames() const
{
    QHash<int,QByteArray> roles;
    roles.insert(AbstractListRoles::SectionRole,"section");
    return roles;
}

AbstractList::AbstractList(const QString &section)
    :section_(section)
{

}

QString AbstractList::getSection() const
{
    return section_;
}
